﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using ForumLibrary;
using ForumLibrary.Data;


namespace ForumGUI
{
    public partial class AddEditThread : Form
    {
        private ClientController    _controller;
        private ForumManager       _manager;
        private String              _prevHeader;
        private bool                _isAdd;
        private String              _forum;

        public AddEditThread(ClientController controller, ref ForumManager manager,
            String forum, bool isAdd, String header = "")
        {
            InitializeComponent();
            _isAdd = isAdd;
            _manager = manager;
            _controller = controller;
            _controller.pushForm(this);
            _forum = forum;
            _prevHeader = header;
            if (!isAdd)
            {
                char[] delimiter = { '~' };
                String toParse="";
                try
                {
                    toParse = _manager.getThreadDetails(_forum, header);
                }
                catch
                {
                    _controller.Logger.Error("Could not get Thread Details");
                    MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
                }
                string[] parser = toParse.Split(delimiter);
                ContentTextBox.Text = parser[1];
            }
            else
                ContentTextBox.Text = "";
            HeaderTextBox.Text = header;
            ConfirmButton.Text = _isAdd ? "Add" : "Edit";
        }

        private bool containHeader(String header)
        {
            String toParse = "";
            try
            {
                toParse = (string)_manager.getThreads(_forum);
            }
            catch
            {
                _controller.Logger.Error("Could not get Thread Headers");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            String[] headers = toParse.Split('~');
            foreach (String s in headers)
                if (s.Equals(header))
                    return true;
            return false;
        }
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(HeaderTextBox.Text))
            {
                MessageBox.Show("Please enter header!!!");
                return;
            }
            if (String.IsNullOrEmpty(ContentTextBox.Text))
            {
                MessageBox.Show("Please enter content!!!");
                return;
            }
            if (containHeader(HeaderTextBox.Text))
            {
                MessageBox.Show("Thread header already exist!!!");
                return;
            }
            String res = "";
            try
            {
                res = (_isAdd) ? _manager.addThread(_controller.Username, _forum, HeaderTextBox.Text, ContentTextBox.Text) :
                                    _manager.editThread(_controller.Username, _forum, _prevHeader, HeaderTextBox.Text, ContentTextBox.Text);
                _controller.Logger.Info(_controller.Username + " Add/Edit Thread");
            }
            catch
            {
                _controller.Logger.Error("Could not Add/Edit Thread");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            this.Dispose(false);
            ThreadsFrame tf = (ThreadsFrame)(_controller.popForm());
            tf.refreshThreads();
            tf.Visible = true;
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            ThreadsFrame tf = (ThreadsFrame)(_controller.popForm());
            tf.refreshThreads();
            tf.Visible = true;
            this.Dispose(false);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void HeaderLabel_Click(object sender, EventArgs e)
        {

        }
    }
}
